package com.radiation.rms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.radiation.rms.common.R;
import com.radiation.rms.common.RMSCommon;
import com.radiation.rms.common.RMSConstant;
import com.radiation.rms.entity.AclUserDepartment;
import com.radiation.rms.entity.Device;
import com.radiation.rms.entity.Role;
import com.radiation.rms.entity.User;
import com.radiation.rms.exception.GuliException;
import com.radiation.rms.service.*;
import com.radiation.rms.vo.AreaVo;
import com.radiation.rms.vo.CountVo;
import com.radiation.rms.vo.DataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 10626
 */
@Service
@Slf4j
public class IndexServiceImpl implements IndexService {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AclUserDepartmentService aclUserDepartmentService;

    @Autowired
    private RMSCommon rmsCommon;

    /**
     * 根据用户名获取用户登录信息
     *
     * @param username
     * @return
     */
    @Override
    public Map<String, Object> getUserInfo(String username) {
        Map<String, Object> result = new HashMap<>();
        User user = userService.selectByUsername(username);
        if (null == user) {
            throw new GuliException(20001, "系统错误");
        }

        //根据用户id获取角色
        List<Role> roleList = roleService.selectRoleByUserId(user.getId());
        List<String> roleNameList = roleList.stream().map(item -> item.getRoleName()).collect(Collectors.toList());
        if (roleNameList.size() == 0) {
            //前端框架必须返回一个角色，否则报错，如果没有角色，返回一个空角色
            roleNameList.add("");
        }

        //根据用户id获取操作权限值
        List<String> permissionValueList = permissionService.selectPermissionValueByUserId(user.getId());
        redisTemplate.opsForValue().set(username, permissionValueList);

        result.put("name", user.getUsername());
        result.put("avatar", "http://bpic.588ku.com/element_origin_min_pic/01/48/90/655744466b77df5.jpg");
        result.put("roles", roleNameList);
        result.put("permissionValueList", permissionValueList);
        return result;
    }


    @Override
    public List<JSONObject> getMenu(String username) {
        User user = userService.selectByUsername(username);
        //根据用户id获取用户菜单权限
        return permissionService.selectPermissionByUserId(user.getId());
    }

    @Override
    public R getDataMap() {
        // TODO  数据控制
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<String> departmentIds = rmsCommon.getDepartmentIds(username);
        List<AreaVo> areaVos = deviceService.getDataMap(departmentIds);
        Map<String, Double[]> map = deviceService.getAreaMap(departmentIds);
        CountVo countVo = deviceService.getCount(departmentIds);
        return R.ok().data("mapvo", areaVos).data("countvo", countVo).data("map", map);
    }
}
